<?php

namespace app\service\impl;

use app\service\AlbulmsService;
use fast\Http;
use think\facade\Log;

/**
 * 洞签平台
 */
class AlbumsMegviiServiceImpl implements AlbulmsService{

    protected $config;

    public function __construct(){
        $service_type=config('capture.platform');
        $this->config=config('capture.'.$service_type);
    }

    public function sys_lists() {
        $this->config['token_array']['api_id']='A001';
        $url=$this->config['url'].'album/listAlbums?'.http_build_query($this->config['token_array']);
        //$data=file_get_contents($url);
        $lists=file_get_contents($url);
        $lists=json_decode($lists,true);
        if($lists['code']===0){
            return $lists['data']['results'];
        }
        return null;
    }

    public function create_album($data){
        $this->config['token_array']['api_id']='A002';
        $url=$this->config['url'].'album/createAlbum?'.http_build_query($this->config['token_array']);
        $data['album_type']=2;
        //$data=array_merge($data,$this->config['token_array']);
        $res=Http::post($url,json_encode($data),[CURLOPT_HTTPHEADER=>['Content-Type:application/json']]);
        Log::info("新增底库：".json_encode($data));
        Log::info("新增底库结果：".$res);
        return $res;
    }

    public function edit_album($data) {
        $this->config['token_array']['api_id']='A004';
        $url=$this->config['url'].'album/updateAlbum?'.http_build_query($this->config['token_array']);
        //$data=array_merge($data,$this->config['token_array']);
        $res=Http::post($url,$data);
        Log::info("修改底库：".json_encode($data));
        Log::info("修改底库结果：".$res);
    }

    public function del_album($data){
        $this->config['token_array']['api_id']='A003';
        $url=$this->config['url'].'album/deleteAlbum?'.http_build_query($this->config['token_array']);
        //$data=array_merge($data,$this->config['token_array']);
        $res=Http::post($url,$data);
        Log::info("删除底库：".json_encode($data));
        Log::info("删除底库结果：".$res);
    }

    public function create_face($data,$album_type=1,$album_id=''){
        $this->config['token_array']['api_id']='A006';
        $url=$this->config['url'].'album/face/createFace?'.http_build_query($this->config['token_array']);
        //$data=array_merge($data,$this->config['token_array']);
        if($album_type==1){
            $album_id=$this->config['album_id'];
        }elseif ($album_type==2){
            $album_id=$this->config['unname_album_id'];
        }else{

        }
        $send_data=[
            'album_id'=>$album_id,
            'img_base64'=>$data->photo,
            'name'=>$data->ywlsh,
            'note'=>$data->name.' '.$data->address.' '.$data->phone,
            'identify_list'=>array(
                'card_type'=>1,
                'card_no'=>$data->idCard
            )
        ];
        $res=Http::post($url,json_encode($send_data),[CURLOPT_HTTPHEADER=>['Content-Type:application/json']]);
        unset($send_data['img_base64']);
        Log::info("添加底库人像：".json_encode($send_data));
        Log::info("添加底库人像结果：".$res);
        return $res;
    }

    public function compare_face($photo,$compare_type=1,$start_time='',$end_time=''){
        $this->config['token_array']['api_id']='F002';
        $url=$this->config['url'].'compare/faceSearch?'.http_build_query($this->config['token_array']);
        //$data=array_merge($data,$this->config['token_array']);

        if($compare_type==2){
            $album_id=[$this->config['unname_album_id']];
        }else if($compare_type==1){
            $album_id=[$this->config['album_id']];
        }else{
            $album_id=$compare_type;
        }
        $send_data=[
            'albums'=>$album_id,
            'album_type'=>2,
            'img_base64'=>$photo,
            'num'=>1,
            'threshold'=>$this->config['threshold'],
        ];
        $res=Http::post($url,json_encode($send_data),[CURLOPT_HTTPHEADER=>['Content-Type:application/json']]);
        unset($send_data['img_base64']);
        //Log::info("比较地址：".$url);
        Log::info("比较人像：".json_encode($send_data));
        Log::info("比较人像结果：".$res);
        return $res;
    }
}